package com.jerry.mingxing.ui.categroybrand;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.jerry.mingxing.R;

import com.jerry.mingxing.bean.CategroyBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/30.
 */
public class CategroyBrandFragment extends Fragment implements CategroyBrandContract.View{
    @Bind(R.id.categroyfragment_right_lv)
    ListView categroyfragmentRightLv;
    @Bind(R.id.categroyfragment_right_gv)
    GridView categroyfragmentRightGv;

    private View view;
    private Activity activity;
    private List<CategroyBean.DataBean.ItemsBean> totallist= new ArrayList<>();
    private List<CategroyBean.DataBean.ItemsBean.ComponentBean.ItemsBean_inner> totalistContent=new ArrayList<>();
    private CategroyTitleAdapter categroyTitleAdapter;
    private CategroyShowAdapter categroyShowAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.categroyfragment_right, container, false);
        ButterKnife.bind(this, view);
        CategroyBrandPresenter brandPresenter=new CategroyBrandPresenter(this);
        brandPresenter.getData();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResponse(CategroyBean cbean) {
        setAdapter(cbean);
        setListener(cbean);
    }
    private void setListener(final CategroyBean cbean) {
        categroyfragmentRightGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //brandgrid view的点击事件
            }
        });
        categroyfragmentRightLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                categroyShowAdapter.reloadData(cbean.getData().getItems().get(position).getComponent().getItemsinner());
            }
        });
    }

    private void setAdapter(CategroyBean cbean) {
        totallist=cbean.getData().getItems();
        categroyTitleAdapter=new CategroyTitleAdapter(totallist,activity) ;
        categroyfragmentRightLv.setAdapter(categroyTitleAdapter);

        totalistContent=cbean.getData().getItems().get(0).getComponent().getItemsinner();
        categroyShowAdapter=new CategroyShowAdapter(totalistContent,activity);
        categroyfragmentRightGv.setAdapter(categroyShowAdapter);
    }

    @Override
    public void onFailure(String s) {

    }
}
